// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.commands.serverCommands;

import zombie.characters.IsoPlayer;
import zombie.commands.AltCommandArgs;
import zombie.commands.CommandArgs;
import zombie.commands.CommandBase;
import zombie.commands.CommandHelp;
import zombie.commands.CommandName;
import zombie.commands.RequiredRight;
import zombie.core.math.PZMath;
import zombie.core.raknet.UdpConnection;
import zombie.iso.IsoChunk;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoWorld;
import zombie.network.GameServer;
import zombie.network.ServerMap;
import zombie.scripting.ScriptManager;
import zombie.scripting.objects.VehicleScript;
import zombie.vehicles.BaseVehicle;
import zombie.vehicles.VehiclesDB2;

@CommandName(
    name = "addvehicle"
)
@AltCommandArgs({@CommandArgs(
        required = {"([a-zA-Z0-9.-]*[a-zA-Z][a-zA-Z0-9_.-]*)"},
        argName = "Script Only"
    ), @CommandArgs(
        required = {"([a-zA-Z0-9.-]*[a-zA-Z][a-zA-Z0-9_.-]*)", "(\\d+),(\\d+),(\\d+)"},
        argName = "Script And Coordinate"
    ), @CommandArgs(
        required = {"([a-zA-Z0-9.-]*[a-zA-Z][a-zA-Z0-9_.-]*)", "(.+)"},
        argName = "Script And Player"
    )})
@CommandHelp(
    helpText = "UI_ServerOptionDesc_AddVehicle"
)
@RequiredRight(
    requiredRights = 60
)
public class AddVehicleCommand extends CommandBase {
    public static final String scriptOnly = "Script Only";
    public static final String scriptPlayer = "Script And Player";
    public static final String scriptCoordinate = "Script And Coordinate";

    public AddVehicleCommand(String username, String userAccessLevel, String command, UdpConnection connection) {
        super(username, userAccessLevel, command, connection);
    }

    @Override
    protected String Command() {
        String string0 = this.getCommandArg(Integer.valueOf(0));
        VehicleScript vehicleScript = ScriptManager.instance.getVehicle(string0);
        if (vehicleScript == null) {
            return "Unknown vehicle script \"" + string0 + "\"";
        } else {
            string0 = vehicleScript.getModule().getName() + "." + vehicleScript.getName();
            int int0;
            int int1;
            int int2;
            if (this.argsName.equals("Script And Player")) {
                String string1 = this.getCommandArg(Integer.valueOf(1));
                IsoPlayer isoPlayer0 = GameServer.getPlayerByUserNameForCommand(string1);
                if (isoPlayer0 == null) {
                    return "User \"" + string1 + "\" not found";
                }

                int0 = PZMath.fastfloor(isoPlayer0.getX());
                int1 = PZMath.fastfloor(isoPlayer0.getY());
                int2 = PZMath.fastfloor(isoPlayer0.getZ());
            } else if (this.argsName.equals("Script And Coordinate")) {
                int0 = PZMath.fastfloor(Float.parseFloat(this.getCommandArg(Integer.valueOf(1))));
                int1 = PZMath.fastfloor(Float.parseFloat(this.getCommandArg(Integer.valueOf(2))));
                int2 = PZMath.fastfloor(Float.parseFloat(this.getCommandArg(Integer.valueOf(3))));
            } else {
                if (this.connection == null) {
                    return "Pass a username or coordinate";
                }

                String string2 = this.getExecutorUsername();
                IsoPlayer isoPlayer1 = GameServer.getPlayerByUserNameForCommand(string2);
                if (isoPlayer1 == null) {
                    return "User \"" + string2 + "\" not found";
                }

                int0 = PZMath.fastfloor(isoPlayer1.getX());
                int1 = PZMath.fastfloor(isoPlayer1.getY());
                int2 = PZMath.fastfloor(isoPlayer1.getZ());
            }

            if (int2 > 0) {
                return "Z coordinate must be 0 for now";
            } else {
                IsoGridSquare isoGridSquare = ServerMap.instance.getGridSquare(int0, int1, int2);
                if (isoGridSquare == null) {
                    return "Invalid location " + int0 + "," + int1 + "," + int2;
                } else {
                    BaseVehicle baseVehicle = new BaseVehicle(IsoWorld.instance.CurrentCell);
                    baseVehicle.setScriptName(string0);
                    baseVehicle.setX((float)int0 - 1.0F);
                    baseVehicle.setY((float)int1 - 0.1F);
                    baseVehicle.setZ((float)int2 + 0.2F);
                    if (IsoChunk.doSpawnedVehiclesInInvalidPosition(baseVehicle)) {
                        baseVehicle.setSquare(isoGridSquare);
                        baseVehicle.square.chunk.vehicles.add(baseVehicle);
                        baseVehicle.chunk = baseVehicle.square.chunk;
                        baseVehicle.addToWorld();
                        VehiclesDB2.instance.addVehicle(baseVehicle);
                        return "Vehicle spawned";
                    } else {
                        return "ERROR: I can not spawn the vehicle. Invalid position. Try to change position.";
                    }
                }
            }
        }
    }
}
